//菜单数据


var redis=require('./Redis');
var db=require('./db');

function Food(name,price,shopId){
    this.name=name;
    this.price=price;
    this.shopId=shopId;
}

//插入数据
Food.prototype.insert=function(callback){
    var obj={};
    obj["name"]=this.name;
    obj["price"]=this.price;
    obj["shopId"]=this.shopId;
    redis.get("food.max_id",function(err,reply){
        if(err) return callback(err);
        var id=parseInt(reply)+1;
        var key="food:"+id;
        obj["id"]=id;
        console.log("插入的数据:"+JSON.stringify(obj));
        redis.set(key,JSON.stringify(obj));
        redis.incr("food.max_id");
        callback(null);
    })
}


//更新数据
Food.prototype.update=function(fId,callback){
    var obj={};
    obj["id"]=fId;
    obj["name"]=this.name;
    obj["price"]=this.price;
    obj["shopId"]=this.shopId;

    var key="food:"+fId;
    redis.set(key,JSON.stringify(obj),function(err,reply){
        console.log("reply:"+reply);
        callback(err);
    });
}


Food.all = function(callback) {
    redis.keys("food:*",function(err,keys){
        if(err) return callback(err);
        redis.mget(keys,function(error,data){
            return callback(error,data);
        })
    })
}

Food.getFoodById=function(id){
    var key="food:"+id;
    redis.get(key,function(err,data){
        return callback(err,data);
    })
}


//根据id查找用户数据
Food.findById=function(id,callback){
    var key="food:"+id;
    redis.get(key,function(err,user){
        callback(err,user);
    })
}

//根据id删除数据
Food.deleteById=function(id,callback){
    var key="food:"+id;
    redis.del(key,function(err,reply){
        console.log("reply:"+reply);
        callback(err);
    })
}


//同步数据
Food.syncData=function(){
    db.query("select * from `food` order by `id` desc",function(err,rows){
        if(err){
            console.error("执行%s语句数据错误,错误详情:"+err,sql);
        }
        else{
            redis.keys("food:*",function(err,keys){
                if(keys){
                    redis.del(keys,function(err,reply){
                        for(var i=0;i<rows.length;i++){
                            var data=rows[i];
                            var key="food:"+data["id"];
                            redis.set(key,JSON.stringify(data));
                        }
                    })
                }
                else{
                    for(var i=0;i<rows.length;i++){
                        var data=rows[i];
                        var key="food:"+data["id"];
                        redis.set(key,JSON.stringify(data));
                    }
                }
            })
        }
        redis.set("food.max_id",(rows&&rows[0])?rows[0].id:0);
    });
}

//同步Redis数据到数据库
Food.syncDataToSql=function(){
    Food.all(function(err,foods){
        if(err) return console.log("从Redis获取用户数据失败");
        db.query('delete from `food` where 1=1',function(e,reply){
           if(err) return console.log("清空food表出错");
           for(var i=0;i<foods.length;i++){
                var u=JSON.parse(foods[i]);
                console.log(foods[i]);
                //插入数据库
                db.query('insert into `food` (`id`,`shopId`,`name`,`price`) values (?,?,?,?)',[u.id,u.shopId,u.name,u.price]);
            }
        });
        
    })
}

module.exports = Food;
